/*
 * Copyright 2008 - 2009 Lars Heuer (heuer[at]semagia.com). All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.semagia.atomico.server.feed.impl;

import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collection;

import static com.semagia.atomico.IConstants.REL_ALTERNATE;
import static com.semagia.atomico.IConstants.REL_FRAGMENTS_FEED;
import static com.semagia.atomico.IConstants.REL_SNAPSHOTS_FEED;

import com.semagia.atomico.MediaType;
import com.semagia.atomico.server.feed.IOutputAwareFeedHandler;
import static com.semagia.atomico.server.feed.impl.FeedWriterUtils.writeFeedStart;
import static com.semagia.atomico.server.feed.impl.FeedWriterUtils.writeEntryStart;

/**
 * {@link IFeedWriter} which serializes a {@link ICollectionFeed}.
 * 
 * @author Lars Heuer (heuer[at]semagia.com) <a href="http://www.semagia.com/">Semagia</a>
 * @version $Rev: 132 $ - $Date: 2012-07-20 16:55:23 +0000 (Fri, 20 Jul 2012) $
 */
final class CollectionFeedWriter implements IFeedWriter {

    private final ICollectionFeed _feed;
    private final IOutputAwareFeedHandler _handler;

    public CollectionFeedWriter(final ICollectionFeed feed, final IOutputAwareFeedHandler handler) {
        _feed = feed;
        _handler = handler;
    }

    /* (non-Javadoc)
     * @see com.semagia.atomico.dm.IWritable#write(java.io.OutputStream)
     */
    @Override
    public void write(final OutputStream out) throws IOException {
        _handler.init(out);
        writeFeedStart(_handler, _feed);
        // Snapshots
        ILinkAwareEntity entry =  _feed.getSnapshotsEntry();
        writeEntryStart(_handler, entry);
        // The mediaTypes are stored since the list is needed for the 
        // fragment entry as well
        final Collection<String> mediaTypes = new ArrayList<String>();
        for (MediaType mediaType: FeedHandlerRegistry.getMediaTypes()) {
            String mt = mediaType.toString();
            // Remember the media type
            mediaTypes.add(mt);
            _handler.link(entry.getLink(), REL_ALTERNATE, mt);
            _handler.link(entry.getLink(), REL_SNAPSHOTS_FEED, mt);
        }
        _handler.endEntry();
        // Fragments
        entry = _feed.getFragmentsEntry();
        writeEntryStart(_handler, entry);
        for (String mt: mediaTypes) {
            _handler.link(entry.getLink(), REL_ALTERNATE, mt);
            _handler.link(entry.getLink(), REL_FRAGMENTS_FEED, mt);
        }
        _handler.endEntry();
        _handler.endFeed();
    }

}
